//
//  WPVersionUpdateView.m
//  health
//
//  Created by apple on 2021/11/16.
//  Copyright © 2021 zglee. All rights reserved.
//

#import "WPVersionUpdateView.h"

@interface WPVersionUpdateView ()
@property (weak, nonatomic) IBOutlet UILabel *content;
/// 提示高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgViewHeight;
@property (nonatomic,  strong) UIWindow *overlayWindow;
@property (nonatomic, strong) NSString *message;
@property (weak, nonatomic) IBOutlet UIButton *btnCancel;
@property (nonatomic, strong) NSString *apkUrl;
@property (weak, nonatomic) IBOutlet UIView *viewTop;


@end

@implementation WPVersionUpdateView

//new
+ (void)showUpdateViewWithMessage:(NSString *)message version:(NSNumber *)version forceUpdate:(BOOL)forceUpdate apkUrl:(NSString *)apkUrl{
    WPVersionUpdateView *view = [[WPVersionUpdateView alloc]initWithMessage:message version:version forceUpdate:forceUpdate];
    if(apkUrl.length > 0){
        view.apkUrl = apkUrl;
    }else{
        view.apkUrl = @"itms-apps://itunes.apple.com/app/id6557068285";
    }
    view.btnCancel.hidden = false;

    if(forceUpdate){
        view.btnCancel.hidden = true;
    }
    
    [view show];
}


- (instancetype)initWithMessage:(NSString *)message version:(nonnull NSNumber *)version forceUpdate:(BOOL)forceUpdate{
    if (self = [super init]) {
        self = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil].firstObject;
        self.frame = SCREEN_MAIN;
        self.message = message;
        [self.overlayWindow addSubview:self];
        
        self.content.text = MJUIEmptyString(self.message)?@"新版app":self.message;

        CGFloat bottomH = forceUpdate ? 0:40;
        self.bgViewHeight.constant = [self.content sizeThatFits:CGSizeMake(SCREEN_WIDTH - 168, MAXFLOAT)].height+290 + bottomH;

    }
    return self;
}
- (void)layoutSublayersOfLayer:(CALayer *)layer{
    [super layoutSublayersOfLayer:layer];
    
//    self.content.text = MJUIEmptyString(self.message)?@"新版app":self.message;
}
- (void)show{
    [self.overlayWindow addSubview:self];
}

- (IBAction)cancle:(UIButton *)sender {
    [self removeFromSuperview];
}

- (IBAction)goUpdate:(UIButton *)sender {
    [UIApplication.sharedApplication openURL:[NSURL URLWithString:self.apkUrl] options:@{} completionHandler:^(BOOL success) {
        
    }];
    NSLog(@"跳转app商城");
}
- (void)layoutSubviews{
    [super layoutSubviews];
    
//    [self.viewTop.superview layoutIfNeeded];
//     [self.viewTop mj_gradientLayerWithColors:@[(id)[[UIColor mj_colorWithHexString:@"#8CFFF2"] mj_colorWithAlpha:0.1].CGColor,(id)[UIColor whiteColor].CGColor] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1)];
    
}
#pragma mark - 懒加载
- (UIWindow *)overlayWindow {
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    if (!window) {
        window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    }
    _overlayWindow = window;
    return _overlayWindow;
}
@end
